class Notification
  include Mongoid::Document
  include Mongoid::Timestamps::Created

  belongs_to :user
  field :read, type: Boolean, default: false
  field :content, type: String

  field :type, type: String
  field :obj, type: Moped::BSON::ObjectId
  field :others, type: Hash

  def self.notification_set
    ret = Object.new
    def ret.send(user, content)
      if @users.nil?
        @users = []
      end
      if !user.nil? && !@users.include?(user)
        n = Notification.create(user: user, content: content)
      end
    end
    yield(ret)
  end

  def self.for(obj)
    ret = Object.new
    def ret.reset(obj)
      @users = []
      @obj = obj.id
      self
    end
    def ret.send(user, type, hsh = {})
      puts "SENDING: #{type}"
      if !user.nil? && !@users.include?(user)
        Notification.create(user: user, type: type, obj: @obj)
        @users << user
        puts "SENT!"
      else
        puts "ELSE!"
      end
    end
    yield(ret.reset(obj))
  end
end
